//
//  CrazyChatExpressionAllView.m
//  webSocket
//
//  Created by 马军凯 on 2019/1/23.
//  Copyright © 2019年 ZH. All rights reserved.
//

#import "CrazyChatExpressionAllView.h"

@interface CrazyChatExpressionAllView () <UIScrollViewDelegate>
{
    UIPageControl *pageControl;
    CGFloat distanceX;
    CGFloat distanceY;
}


@end

@implementation CrazyChatExpressionAllView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, CRAZY_SCREEN_WIDTH, 200)];
        self.scrollView.showsVerticalScrollIndicator = NO;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        self.scrollView.bounces = NO;
        self.scrollView.delegate = self;
        self.scrollView.pagingEnabled = YES;
        [self addSubview:self.scrollView];
        
        pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 200, CRAZY_SCREEN_WIDTH, 40)];
        pageControl.numberOfPages = 2;
        pageControl.userInteractionEnabled = NO;
        pageControl.pageIndicatorTintColor = CRAZY_PAGECONTROL_NO_COLOR;
        pageControl.currentPageIndicatorTintColor = CRAZY_PAGECONTROL_YES_COLOR;
        pageControl.hidden = YES;
        [self addSubview:pageControl];
        
        distanceX = (CRAZY_SCREEN_WIDTH - 280) / 8;
        distanceY = 40 / 5;
    }
    return self;
}






- (void)setDataArray:(NSMutableArray *)dataArray {
    _dataArray = dataArray;
    //移除所有子视图
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    if (dataArray.count % 28 == 0) {
        self.scrollView.contentSize = CGSizeMake(CRAZY_SCREEN_WIDTH * (dataArray.count / 28), 0);
        pageControl.numberOfPages = dataArray.count / 28;
    } else {
        self.scrollView.contentSize = CGSizeMake(CRAZY_SCREEN_WIDTH * (dataArray.count / 28 + 1), 0);
        pageControl.numberOfPages = dataArray.count / 28 + 1;
    }
    if (dataArray.count <= 28) {
        pageControl.hidden = YES;
    } else {
        pageControl.hidden = NO;
    }
    
    CGFloat x = distanceX;
    CGFloat y = distanceY;
    CGFloat width = 40;
    CGFloat height = 40;
    
    for (int i = 0; i < dataArray.count; i++) {
        CrazyChatExpressionModel *model = dataArray[i];
        if ([model.name isEqualToString:@"删除"]) {
            CGFloat deleteX = (i == 27) ? 0.00 : (i == 55) ? CRAZY_SCREEN_WIDTH : (i == 83) ? (CRAZY_SCREEN_WIDTH * 2) : (CRAZY_SCREEN_WIDTH * 3);
            UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            deleteBtn.frame = CGRectMake(deleteX + CRAZY_SCREEN_WIDTH - distanceX - width, 200 - distanceY - width, width, width);
            [deleteBtn addTarget:self action:@selector(clickDeleteBtn:) forControlEvents:UIControlEventTouchUpInside];
            [deleteBtn setImage:[UIImage imageNamed:@"CrazyChat_delete.png"] forState:UIControlStateNormal];
            [self.scrollView addSubview:deleteBtn];
            x = distanceX + CRAZY_SCREEN_WIDTH * ((i + 1) / 28);
            y = distanceY;
        } else {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(x, y, width, height);
            btn.tag = 1000 + i;
            [btn addTarget:self action:@selector(clickInputExpression:) forControlEvents:UIControlEventTouchUpInside];
            [btn setImage:[UIImage imageNamed:model.image] forState:UIControlStateNormal];
            [self.scrollView addSubview:btn];
            if ((i + 1) % 7 == 0) {
                if ((i + 1) % 28 == 0) {
                    y = distanceY;
                } else {
                    y = y + height + distanceY;
                }
                x = CRAZY_SCREEN_WIDTH * ((i + 1) / 28) + distanceX;
            } else {
                x = x + width + distanceX;
            }
        }
    }
}

#pragma mark 点击表情
- (void)clickInputExpression:(UIButton *)sender {
    CrazyChatExpressionModel *model = self.dataArray[sender.tag - 1000];
    if (self.delegate && [self.delegate respondsToSelector:@selector(CrazyChatSelectExpression:)]) {
        [self.delegate CrazyChatSelectExpression:model];
    }
}

#pragma mark 删除
- (void)clickDeleteBtn:(UIButton *)btn {
    if (self.delegate && [self.delegate respondsToSelector:@selector(CrazyChatDeleteExpression)]) {
        [self.delegate CrazyChatDeleteExpression];
    }
}


#pragma mark UIScrollView
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    pageControl.currentPage = scrollView.contentOffset.x / CRAZY_SCREEN_WIDTH;
}

@end













